package controller.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Datentraeger;
import service.DatentraegerService;
import service.Service;
import service.util.ServiceException;
import service.util.ServiceFactory;
import controller.util.MessageHolder;
import dao.util.DaoContextEnum;

/**
 * Servlet implementation class DatDetailServlet
 */
@SuppressWarnings("serial")
public class DatDetailServlet extends HttpServlet {
       
    Datentraeger actualDatentraeger = null;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		MessageHolder holder = new MessageHolder();
		try{
			Service<Datentraeger> service = (DatentraegerService)ServiceFactory.getServiceForName("DatentraegerService", DaoContextEnum.MYSQL);  
			if((request.getParameter("dat")==null || (request.getParameter("dat")).equals("0")) && (request.getAttribute("dat")==null || request.getAttribute("dat").equals("0"))){
				actualDatentraeger = new Datentraeger("new Datentraeger");
			}
			else{
				String dat = request.getParameter("dat");
				if(dat==null){
					dat=(String) request.getAttribute("dat");
				}
				actualDatentraeger = (((DatentraegerService) service).getDatentraegerById(dat));  
			}
			if(actualDatentraeger!=null && actualDatentraeger.getName()!=null){
				request.setAttribute("actualDatentraeger", actualDatentraeger);
				request.getRequestDispatcher("datDetail.jsp").forward(request, response);
				return;
			}
		} catch (ServiceException e){
			holder.setMessage(e.getMessage());
			request.setAttribute("actualDatentraeger", actualDatentraeger);
			request.setAttribute("message", holder);
			request.getRequestDispatcher("datDetail.jsp").forward(request, response);
			return;
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		MessageHolder holder = new MessageHolder();
		try
		{
			DatentraegerService service = ((DatentraegerService) ServiceFactory.getServiceForName("DatentraegerService", DaoContextEnum.MYSQL));
			String id = request.getParameter("id");
			if(id==null || id.equals("null") || id.isEmpty()){
				actualDatentraeger = null;
			} else {
				actualDatentraeger = service.getDatentraegerById(id);
			}
			
			if(request.getParameter("DatNameOk")!=null){
				actualDatentraeger.setName(request.getParameter("DatName"));
				service.saveToDatabase(actualDatentraeger);
				request.setAttribute("dat", id);
				
			}else if(request.getParameter("allOk")!=null){
				actualDatentraeger.setName(request.getParameter("DatName"));
				service.saveToDatabase(actualDatentraeger);
				request.setAttribute("dat", id);
				
			}else if(request.getParameter("save")!=null){
				if((service.getDatentraegerForName(request.getParameter("DatName")))==null){
					actualDatentraeger = new Datentraeger(request.getParameter("DatName"));
					service.saveToDatabase(actualDatentraeger);
					holder.setMessage("Datentraeger gespeichert!");
					response.sendRedirect("DatDetailServlet?dat="+actualDatentraeger.getId().toString());
					return;
				}else{
					holder.setMessage("Datentraeger bereits vorhanden!");
					actualDatentraeger = new Datentraeger("new Datentraeger");
				}
				request.setAttribute("message", holder);
				
			}else if(request.getParameter("new")!=null){
				doGet(request, response);
				return;
				
			}else if(request.getParameter("clear")!=null){
				if(service.deleteDatentraeger(actualDatentraeger)==1){
					holder.setMessage("Datentraeger gelöscht");
					request.setAttribute("message", holder);
					doGet(request, response);
					return;
				}
				holder.setMessage("Datentraeger konnte nicht gelöscht werden");
				request.setAttribute("message", holder);
				doGet(request, response);
				return;
				
			}else {
				holder.setMessage("Sonderfehler!");
				request.setAttribute("message", holder);
				doGet(request, response);
				return;
			}

			request.setAttribute("actualDatentraeger", actualDatentraeger);
			
			doGet(request, response);
			
		} catch (Exception e)
		{
			holder.setMessage(e.getMessage());
			request.setAttribute("actualDatentraeger", actualDatentraeger);
			request.setAttribute("message", holder);
			request.getRequestDispatcher("datDetail.jsp").forward(request, response);
			return;
		} 
	}
}
